package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.base.BaseController;
import com.shop.entity.Coupon;
import com.shop.entity.User;
import com.shop.result.ResultVO;
import com.shop.service.CouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/coupon")
public class CouponController extends BaseController {

    private final CouponService couponsService;

    /**
     * 获取优惠券
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 数据
     */
    @RequestMapping("/listCoupon")
    public ResultVO<PageInfo<Coupon>> listCoupon(@RequestParam("pageNo") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<Coupon> list = couponsService.listCoupon(pageNum, pageSize);
        return success(list);
    }

    @RequestMapping("/listCouponDate")
    public ResultVO<List<Coupon>> listCouponDate(@RequestParam("end_date") LocalDate end_date) {
        if (end_date == null) {
            return error("请选择日期");
        }
        List<Coupon> list = couponsService.listCouponDate(end_date);
        return success(list);
    }

    @RequestMapping("/listCouponByStatus")
    public ResultVO<List<Coupon>> listCouponByStatus() {
        List<Coupon> list = couponsService.listCouponByStatus();
        return success(list);
    }

    /**
     * 根据id获取优惠券
     * @param id 优惠券id
     * @return 数据
     */
    @GetMapping("/getCouponById")
    public Coupon getCouponById(@RequestParam("id") Integer id) {
        return couponsService.getCouponById(id);
    }

    /**
     * 添加优惠券
     * @param coupon 优惠券
     */
    @PostMapping("/addCoupon")
    public ResultVO<Coupon> addCoupon(@RequestBody Coupon coupon) {
        couponsService.addCoupon(coupon);
        return success(coupon);
    }

    /**
     * 删除优惠券
     * @param id 优惠券id
     */
    @GetMapping("/deleteCoupon")
    public void deleteCoupon(@RequestParam("id") Integer id) {
        couponsService.deleteCoupon(id);
    }

    /**
     * 修改优惠券
     * @param coupon 优惠券
     */
    @GetMapping("/updateCoupon")
    public void updateCoupon(@RequestBody Coupon coupon) {
        couponsService.updateCoupon(coupon);
    }

    /**
     * 获取总数
     * @return 数量
     */
    @GetMapping("/getTotal")
    public int getTotal() {
        return couponsService.getTotal();
    }
}
